#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QFileDialog>
#include <QMessageBox>
#include <QSplitter>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QTextStream>
#include <QFile>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent) {
    setWindowTitle("ELF Parser");
    resize(1000, 700);
    
    createActions();
    createMenus();
    createToolBar();
    createStatusBar();
    
    // Create central widget
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);
    
    // Create splitter
    QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
    
    // Create tree view
    m_treeView = new QTreeView(this);
    m_model = new ElfModel(this);
    m_treeView->setModel(m_model);
    m_treeView->setHeaderHidden(false);
    
    // Create detail view
    m_detailView = new DetailView(this);
    
    // Add widgets to splitter
    splitter->addWidget(m_treeView);
    splitter->addWidget(m_detailView);
    splitter->setStretchFactor(0, 1);
    splitter->setStretchFactor(1, 2);
    
    layout->addWidget(splitter);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);
    
    // Enable drag and drop
    setAcceptDrops(true);
    
    // Connect signals
    connect(m_treeView, &QTreeView::clicked, this, &MainWindow::onTreeViewClicked);
}

void MainWindow::createActions() {
    m_openAction = new QAction("Open", this);
    m_openAction->setShortcut(QKeySequence::Open);
    m_openAction->setStatusTip("Open ELF file");
    connect(m_openAction, &QAction::triggered, this, &MainWindow::openFile);
    
    m_exportJsonAction = new QAction("Export JSON", this);
    m_exportJsonAction->setStatusTip("Export as JSON");
    connect(m_exportJsonAction, &QAction::triggered, this, &MainWindow::exportJson);
    
    m_exportTextAction = new QAction("Export Text", this);
    m_exportTextAction->setStatusTip("Export as text");
    connect(m_exportTextAction, &QAction::triggered, this, &MainWindow::exportText);
    
    m_exitAction = new QAction("Exit", this);
    m_exitAction->setShortcut(QKeySequence::Quit);
    connect(m_exitAction, &QAction::triggered, this, &QMainWindow::close);
    
    m_aboutAction = new QAction("About", this);
    connect(m_aboutAction, &QAction::triggered, this, &MainWindow::showAbout);
}

void MainWindow::createMenus() {
    QMenu *fileMenu = menuBar()->addMenu("File");
    fileMenu->addAction(m_openAction);
    fileMenu->addSeparator();
    fileMenu->addAction(m_exportJsonAction);
    fileMenu->addAction(m_exportTextAction);
    fileMenu->addSeparator();
    fileMenu->addAction(m_exitAction);
    
    QMenu *helpMenu = menuBar()->addMenu("Help");
    helpMenu->addAction(m_aboutAction);
}

void MainWindow::createToolBar() {
    QToolBar *toolbar = addToolBar("Main");
    toolbar->addAction(m_openAction);
    toolbar->addSeparator();
    toolbar->addAction(m_exportJsonAction);
    toolbar->addAction(m_exportTextAction);
}

void MainWindow::createStatusBar() {
    statusBar()->showMessage("Ready");
}

void MainWindow::openFile() {
    QString filename = QFileDialog::getOpenFileName(this, "Open ELF File", "", 
                                                   "ELF Files (*.elf);;All Files (*)");
    if (!filename.isEmpty()) {
        loadFile(filename);
    }
}

void MainWindow::loadFile(const QString &filename) {
    m_model->loadElf(filename);
    
    QFileInfo info(filename);
    statusBar()->showMessage(QString("Loaded: %1").arg(info.fileName()));
}

void MainWindow::exportJson() {
    QString filename = QFileDialog::getSaveFileName(this, "Export JSON", "", 
                                                     "JSON Files (*.json);;All Files (*)");
    if (!filename.isEmpty()) {
        QFile file(filename);
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream stream(&file);
            stream << m_model->toJson();
            file.close();
            statusBar()->showMessage("Exported to JSON");
        }
    }
}

void MainWindow::exportText() {
    QString filename = QFileDialog::getSaveFileName(this, "Export Text", "", 
                                                     "Text Files (*.txt);;All Files (*)");
    if (!filename.isEmpty()) {
        QFile file(filename);
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream stream(&file);
            stream << m_model->toText();
            file.close();
            statusBar()->showMessage("Exported to text");
        }
    }
}

void MainWindow::showAbout() {
    QMessageBox::about(this, "About ELF Parser", 
                       "ELF Parser v1.0.0\n\n"
                       "A simple ELF file parser with Qt GUI.\n"
                       "Supports 32-bit and 64-bit ELF files.");
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
    if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    }
}

void MainWindow::dropEvent(QDropEvent *event) {
    QList<QUrl> urls = event->mimeData()->urls();
    if (!urls.isEmpty()) {
        QString filename = urls.first().toLocalFile();
        loadFile(filename);
    }
}

void MainWindow::onTreeViewClicked(const QModelIndex &index) {
    m_detailView->showDetails(index);
}
